package net.shenru.lbs;

import net.shenru.base.ConstConfig;
import net.shenru.base.SystemConfig;
import net.shenru.os.utlis.SrToast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKLocationManager;

import android.content.Context;

/**
 * 对BMapManager进行包装
 */
public class BaiduMapManager implements MKGeneralListener {

	private static String BAIDU_MAP_KEY = SystemConfig.BAIDU_MAP_KEY;
	private static final int UPDATA_LOCATION_SPACE = ConstConfig.UPDATA_LOCATION_SPACE;
	private static final int UPDATA_LOCATION_TIME = ConstConfig.UPDATA_LOCATION_TIME;

	private static BaiduMapManager instance;
	private BMapManager mBMapManager;

	private boolean isInitSuccess = false;

	private static MKGeneralListener mListener;
	private static Context mContext;

	private BaiduMapManager() {
		mBMapManager = new BMapManager(mContext);
		isInitSuccess = mBMapManager.init(BAIDU_MAP_KEY, this);
		if(isInitSuccess){
			// 这里是10米 5分钟
			mBMapManager.getLocationManager().setNotifyInternal(UPDATA_LOCATION_SPACE, UPDATA_LOCATION_TIME);
		}else{
			SrToast.show(mContext, "百度地图初始化失败");
		}
	}

	public static synchronized BaiduMapManager getInstance(Context context, MKGeneralListener listener) {
		mContext = context;
		mListener = listener;
		if (instance == null) {
			instance = new BaiduMapManager();
		}
		return instance;
	}

	public boolean start() {
		return mBMapManager.start();
	}

	// 停止。如果初始化失败。停止成功
	public boolean stop() {
		return mBMapManager.stop();
	}

	public void destroy() {
		mBMapManager.destroy();
	}

	@Override
	public void onGetNetworkState(int error) {
		if (mListener != null) {
			SrToast.show(mContext, "baidu map init error：" + error);
			mListener.onGetNetworkState(error);
		}
	}

	@Override
	public void onGetPermissionState(int error) {
		if (mListener != null) {
			SrToast.show(mContext, "baidu map init error：" + error);
			mListener.onGetPermissionState(error);
		}
	}

	/**
	 * 返回BMapManager
	 */
	public BMapManager getBMapManager() {
		return mBMapManager;
	}

	/**
	 * 返回BLocationManager
	 * 
	 * @return
	 */
	public MKLocationManager getLocationManager() {
		
		return mBMapManager.getLocationManager();
	}

	public boolean isInitSuccess() {
		return isInitSuccess;
	}

	public void setInitSuccess(boolean isInitSuccess) {
		this.isInitSuccess = isInitSuccess;
	}

}
